<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AWS Lambda Request</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            background-color: #f7f7f7;
        }
        h1 {
            margin-top: 40px;
        }
        .container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .form {
            width: 400px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #fff;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }
        .form label {
            display: block;
            margin-bottom: 10px;
        }
        .form input[type="text"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ddd;
            border-radius: 3px;
        }
        .form textarea {
            width: 100%;
            height: 100px;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ddd;
            border-radius: 3px;
        }
        .form button {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .popup {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            padding: 20px;
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            z-index: 9999;
            display: none;
        }
        .popup.success {
            background-color: #4CAF50;
            color: white;
        }
        .popup.error {
            background-color: #FF5252;
            color: white;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="form">
            <h1>亚马逊云AWS重置密码</h1>
            <label for="instanceID">服务器实例ID:</label>
            <input type="text" id="instanceID" th:value="${dto.serverInstanceId}" readonly="readonly">
            <label for="instanceAccount">服务器账号:</label>
            <input type="text" id="instanceAccount" value="MINIEASE">
            <label for="newPass">新密码:</label>
            <input type="text" id="newPass" placeholder="请输入新密码，必须同时包含：大写字母/小写字母/阿拉伯数字">
            <button onclick="sendRequest()">确定重置</button>
        </div>
    </div>

    <div id="popup" class="popup"></div>

    <script>
        function sendRequest() {
            hidePopup();

            var instanceID = document.getElementById('instanceID').value;
            var instanceAccount = document.getElementById('instanceAccount').value;
            if(instanceAccount == '') {
            	alert('服务器账号不能为空');
            	return;
            }
            var newPass = document.getElementById('newPass').value;
            if(!hasUppercase(newPass) || !hasLowercase(newPass) || !hasNumber(newPass)) {
            	alert('密码格式有误，必须同时带有大小写字母/数字');
            	return;
            }

            var url = 'https://0575gapna0.execute-api.cn-northwest-1.amazonaws.com.cn/resetpass';

            var data = {
                instance_id: instanceID,
                command_parameters: {
                    commands: ['net user ' + instanceAccount + ' ' + newPass]
                }
            };

            fetch(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            })
            .then(response => response.json())
            .then(result => {
                console.log(result);
                showSuccessPopup();
            })
            .catch(error => {
                console.error(error);
                showErrorPopup();
            });
        }

        function showSuccessPopup() {
            var popup = document.getElementById('popup');
            popup.innerText = '操作成功!';
            popup.classList.add('success');
            popup.style.display = 'block';
            setTimeout(hidePopup, 3000);
        }

        function showErrorPopup() {
            var popup = document.getElementById('popup');
            popup.innerText = 'Error sending request!';
            popup.classList.add('error');
            popup.style.display = 'block';
            setTimeout(hidePopup, 3000);
        }

        function hidePopup() {
            var popup = document.getElementById('popup');
            popup.innerText = '';
            popup.classList.remove('success', 'error');
            popup.style.display = 'none';
        }
        
        function hasUppercase(str)
        {
            var result = str.match(/^.*[A-Z]+.*$/);
            if(result==null) return false;
            return true;
        }
        
        function hasLowercase(str)
        {
            var result = str.match(/^.*[a-z]+.*$/);
            if(result==null) return false;
            return true;
        }
        
        function hasNumber(str)
        {
            var result = str.match(/^.*[0-9]+.*$/);
            if(result==null) return false;
            return true;
        }
    </script>
</body>
</html>
